$(function() {
    begin()
    getTitleBC();

    function getTitleBC() {
        $.get('http://193.112.55.79:9090/api/getbaicaijiatitle', function(res) {
            var aa = res.result;
            var html = template('baiCaiTemp', { arr: aa });
            // console.log(html);
            $('.baicaitable').html(html);
            var lis = $('.baicaitable li');
            var length = lis.length;

            var long = 0;
            for (var i = 0; i <= lis.length; i++) {
                long += $(lis[i]).width();
            }
            // console.log(long);

            $('.baicaitable').width(long + 1);
            var myScroll = new IScroll('.baicaijianav', { scrollX: true, scrollY: false });
            // var tach = true;
            $('.baicaitable').on('tap', 'li', function(e) {
                for (i = 0; i < length; i++) {
                    lis.removeClass('active');
                }
                $(this).addClass('active');
                // console.log($(this));

                var str = $(this);
                var titleid = str.attr('name');
                //请求页面数据
                $.ajax({
                    type: 'get',
                    url: 'http://193.112.55.79:9090/api/getbaicaijiaproduct',
                    data: {
                        titleid: titleid
                    },
                    dataType: 'json',
                    success: function(res) {
                        // console.log(res);
                        var arr2 = res.result;
                        // arr2[arr2.length] = length;
                        // arr2.append(length);
                        console.log(arr2);

                        // console.log(str);
                        var str = '';
                        for (var i = 0; i < length; i++) {
                            if (i == titleid) {
                                str += "<ul class='clearfix active'>" + "</ul>";
                            } else {
                                str += "<ul class='clearfix'>" + "</ul>";
                            }

                        }
                        // console.log(str);
                        $('.content').html(str);
                        var html = template('baiCaicount', {
                                arr1: arr2,
                            })
                            // console.log(html);
                        $('.content .active').html(html);
                    }
                });
            })
        })
    }

    function begin() {
        $.get('http://193.112.55.79:9090/api/getbaicaijiatitle', function(res) {
            var aa = res.result;
            var long = aa.length;
            var str = '';
            for (var i = 0; i < long; i++) {
                if (i == 0) {
                    str += "<ul class='clearfix active'>" + "</ul>";
                } else {
                    str += "<ul class='clearfix'>" + "</ul>";
                }

            }
            $('.content').html(str);
            $.ajax({
                type: 'get',
                url: 'http://193.112.55.79:9090/api/getbaicaijiaproduct',
                data: {
                    titleid: 0
                },
                dataType: 'json',
                success: function(res) {
                    console.log(res);
                    var arr2 = res.result;
                    // arr2[arr2.length] = length;
                    // arr2.append(length);
                    console.log(arr2);

                    var html = template('baiCaicount', {
                            arr1: arr2,
                        })
                        // console.log(html);
                    $('.content .active').html(html);
                }
            });
        })

    }


})